<?php
/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*
* @author chuck
*/

class Config
{
    public static $redis_configs = array(
        array(
            'host' => '127.0.0.1',
            'port' => 39100,
            'database' => 6,
        ),
    );
    public static $default_name = array(
        "Peter", "Charles", "Mark", "Bill", "Vincent", "William", "Josep", "James", "Henry", "Gary", "Martin"
    );

    public static $default_avatar_men = array(
        "http://test-face-render.deja.mozat.net/faces/09e6eb93fc8cf1b3513a5b3f494ed706243bb0f4/avatar/128/normal/0.png"
    );
    public static $default_avatar_men_head = array(
        "http://test-face-render.deja.mozat.net/faces/09e6eb93fc8cf1b3513a5b3f494ed706243bb0f4/avatar/128/headonly/0.png"
    );
    public static $IAPVerify_url_sb = "https://sandbox.itunes.apple.com/verifyReceipt";

    public static $IAPVerify_url = "https://buy.itunes.apple.com/verifyReceipt";

    public static $public_apis = array(
//        "requestVcode" => "http://testapi.deja.mozat.net/account/requestVcode?_appId=linkus",
//        "verifyPhoneNumber" => "http://testapi.deja.mozat.net/account/verifyPhoneNumber?_appId=linkus",
        "gettoken" => "https://testapi.deja.mozat.net/auth/gettoken",
        "me" => "https://testapi.deja.mozat.net/profile/me",
        "getProfiles" => "https://testapi.deja.mozat.net/profile/getProfilesV2",
        "getFriends" => "https://testapi.deja.mozat.net/friend/getFriendsv2",
        "getAvatar" => "https://testapi.deja.mozat.net/profile/getSmiley",
        "pushMessage" => "https://testapi.deja.mozat.net/message/push",
        "addFriend" => "https://testapi.deja.mozat.net/friend/addFriends?_appId=linkus&_userId=",
        "deleteFriend" => "https://testapi.deja.mozat.net/friend/deleteFriends?_appId=linkus&_userId="
    );

    public static $defaultDevice = "windows_default_device";

    public static $client_id = "shooter";

    public static $client_secret = "45e39de779bb13778457ab5c93949ed5";

    public static $settingVerison = "1.0";

    public static $stageCount = 54;

    public static $debug = true;

    public static function exception_error_handler($errno, $errstr, $errfile, $errline)
    {
        throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
    }

    private static $_redis_objects_array = array();

    public static function getRedisClient($instanceInt = 0)
    {
        Yii::setPathOfAlias('Predis', 'D:\xampp\htdocs\game\g_shooter\protected\extensions\predis\lib\Predis');
        // 0:cache
        // 1:for permanent connection
        // 2:for permanent connection
        $redisObj = null;
        if (!isset(self::$_redis_objects_array[$instanceInt])) {
            $redisObj = new Predis\Client(array(
                'host' => self::$redis_configs[$instanceInt]['host'],
                'port' => self::$redis_configs[$instanceInt]['port'],
                'database' => self::$redis_configs[$instanceInt]['database'],
            ));
            self::$_redis_objects_array[$instanceInt] = $redisObj;
        } else
            $redisObj = self::$_redis_objects_array[$instanceInt];
        return $redisObj;
    }

    public static $gameFileLc = "D:\\xampp\\htdocs\\game\\g_shooter\\data\\game\\";
    public static $stageFileLc = "D:\\xampp\\htdocs\\game\\g_shooter\\data\\stage\\";
    public static $csvFileLc = "D:\\xampp\\htdocs\\game\\g_shooter\\data\\csv\\";
    public static $logFileLc = "D:\\xampp\\htdocs\\game\\g_shooter\\protected\\runtime\\";
    public static function getItemInitFile()
    {
        return array("PropItem" => self::$csvFileLc . "PropItem.csv",
            "PropWeapon" => self::$csvFileLc . "PropWeapon.csv",
            "IAPItem" => self::$csvFileLc . "IAPItem.csv"
        );
    }

    public static function getSettingFile()
    {
        return array("PropBullet" => self::$gameFileLc . "PropBullet.txt",
            "PropBullet_bind" => self::$gameFileLc . "PropBullet_bind.txt",
            "PropCharacter" => self::$gameFileLc . "PropCharacter.txt",
            "PropCharacter_bind" => self::$gameFileLc . "PropCharacter_bind.txt",
            "PropEffect" => self::$gameFileLc . "PropEffect.txt",
            "PropItem" => self::$gameFileLc . "PropItem.txt",
            "PropNPC" => self::$gameFileLc . "PropNPC.txt",
            "PropNPC_bind" => self::$gameFileLc . "PropNPC_bind.txt",
            "PropObj" => self::$gameFileLc . "PropObj.txt",
            "PropObj_bind" => self::$gameFileLc . "PropObj_bind.txt",
            "PropPhysics" => self::$gameFileLc . "PropPhysics.txt",
            "PropWeapon" => self::$gameFileLc . "PropWeapon.txt",
            "Ch1_Stage1" => self::$stageFileLc . "Ch1_Stage1.txt",
            "Ch1_Stage2" => self::$stageFileLc . "Ch1_Stage2.txt",
            "Ch1_Stage3" => self::$stageFileLc . "Ch1_Stage3.txt",
            "Ch1_Stage4" => self::$stageFileLc . "Ch1_Stage4.txt",
            "Ch1_Stage5" => self::$stageFileLc . "Ch1_Stage5.txt",
            "Ch1_Stage6" => self::$stageFileLc . "Ch1_Stage6.txt",
            "Ch1_Stage7" => self::$stageFileLc . "Ch1_Stage7.txt",
            "Ch1_Stage8" => self::$stageFileLc . "Ch1_Stage8.txt",
            "Ch1_Stage9" => self::$stageFileLc . "Ch1_Stage9.txt",
            "Ch1_Stage10" => self::$stageFileLc . "Ch1_Stage10.txt",
            "Ch1_Stage11" => self::$stageFileLc . "Ch1_Stage11.txt",
            "Ch1_Stage12" => self::$stageFileLc . "Ch1_Stage12.txt",
            "Ch1_Stage13" => self::$stageFileLc . "Ch1_Stage13.txt",
            "Ch1_Stage14" => self::$stageFileLc . "Ch1_Stage14.txt",
            "Ch1_Stage15" => self::$stageFileLc . "Ch1_Stage15.txt",
            "Ch1_Stage16" => self::$stageFileLc . "Ch1_Stage16.txt",
            "Ch1_Stage17" => self::$stageFileLc . "Ch1_Stage17.txt",
            "Ch1_Stage18" => self::$stageFileLc . "Ch1_Stage18.txt"
        );
    }
}

function parse_http_head($str)
{
    $result = array();
    // Split into lines
    $lines = explode("\r\n", $str);
    // Handle response line
    $line = explode(' ', array_shift($lines), 3);
    $version = explode('/', $line[0]);
    $result['version'] = (float)$version[1];
    $result['code'] = (int)$line[1];
    $result['text'] = $line[2];

    // Parse headers
    $result['headers'] = array();
    while ($line = trim(array_shift($lines))) {
        list($name, $val) = explode(':', $line, 2);
        $name = strtolower(trim($name)); // Header names are case-insensitive, so convert them all to lower case so we can easily use isset()
        if (isset($result['headers'][$name])) { // Some headers (like Set-Cookie:) may appear more than once, so convert them to an array if necessary
            $result['headers'][$name] = (array)$result['headers'][$name];
            $result['headers'][$name][] = trim($val);
        } else {
            $result['headers'][$name] = trim($val);
        }
    }
    return $result;
}

function sortByRank($a, $b)
{
    return $a->rank < $b->rank;
}

function sortByScore($a, $b)
{
    return ($a->score < $b->score) ? 1 : -1;
}

function sortById($a, $b)
{
    return $a->id > $b->id;
}
function arrayToKV($arr)
{
    $result = "";
    foreach($arr as $key=>$val)
    {
        if(is_array($val))
        {
            $result=$result . $key . "=" . implode(",",$val) . "&";
        }else
        {
            $result=$result . $key . "=" . $val . "&";
        }
    }
    if(!empty($result))
    {
        $result = substr($result,0,strlen($result)-1);
    }
    return $result;
}